package com.ludomancy.archeologyrpg;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Vector;

import com.ludomancy.gamelib.ImageManipulation;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Utils;

public abstract class Entity
{
	// entity sprites
	public static final int ALIEN_BASE_ON = 1;
	public static final int DWELLING = 2;
	public static final int GENERATOR_INACTIVE = 3;
	public static final int GENERATOR_ACTIVE = 4;
	public static final int ALIEN_BASE_OFF = 5;
	public static final int ROCKET = 0;
	public static final int ALIEN_FUNGUS = 6;
	public static final int BUILD_SITE = 7;
	public static final int BEACON = 8;
	public static final int ALIEN_FUNGUS_SPAWNER = 9;
	public static final int OXYGEN = 10;
	public static final int RUIN = 11;
	public static final int SWAMP = 12;
	
	public static ImageStrip sprites;
	public static ImageStrip graySprites;
	
	public int px, py;
	public boolean dead = false;
	public boolean hidden = false;
	public int revelationTime = 0;
	
	public Entity(Point p)
	{
		this(p.x, p.y);
	}
	
	public Entity(int tx, int ty)
	{
		this.px = tx + Game.TILE_SIZE/2;
		this.py = ty + Game.TILE_SIZE/2;
		if(sprites == null)
		{
			sprites = new ImageStrip(ResourceManager.getImage("entities.png"), Game.ENTITY_TILE_SIZE, Game.ENTITY_TILE_SIZE);
		}
		
		if(graySprites == null)
		{
			Image back = ResourceManager.getImage("entities.png");
			BufferedImage bi = ImageManipulation.convertToGrayscale(ImageManipulation.toBufferedImage(back));
			graySprites = new ImageStrip(bi, Game.ENTITY_TILE_SIZE, Game.ENTITY_TILE_SIZE);
		}
	}
	
	public void explore()
	{
		revelationTime = Math.max(0, revelationTime-Game.TIME_PER_UPDATE);
	}
	
	public boolean isVisible()
	{
		return !hidden && revelationTime == 0;
	}
	
	public Vector getCollisions()
	{
		return getCollisions(px, py, Game.ENTITY_TILE_SIZE);
	}
	
	public Vector getCollisions(int x, int y, int radius)
	{
		Vector ret = new Vector();
		for (int i = 0; i < Game.game.entities.size(); i++)
		{
			Entity entity = (Entity) Game.game.entities.elementAt(i);
			if(entity != this && Math.sqrt( (entity.px - x)*(entity.px - x)+(entity.py - y)*(entity.py - y) ) <= radius)
			{
				ret.add(entity);
			}
		}
		return ret;
	}
	
	protected void paintDefaultIcon(Graphics g, int spriteId, int x, int y)
	{
		ImageStrip source = isActive() ? sprites : graySprites;
		source.paint(g, spriteId, x, y, ImageStrip.ANCHOR_HCENTER|ImageStrip.ANCHOR_VCENTER);
	}
	
	public boolean isActive()
	{
		return true;
	}
	
	public abstract String getActionVerb();
	
	public abstract void update(boolean running, boolean activated);
	
	public void paint(Graphics g)
	{
		paint(g, px, py);
	}
	
	public void paintBar(Graphics g, int value, int max)
	{
		GameTheme.theme.paintBar(g, px, py+Game.ENTITY_TILE_SIZE, value, max);
	}
	
	public abstract void paint(Graphics g, int x, int y);

	public abstract void paintInfo(Graphics g);
	
	public void paintVerb(Graphics g)
	{
		if(isActive())
		{
			g.setColor(Color.WHITE);
			g.setFont(GameTheme.theme.tinyFont);
			String verb = getActionVerb();
			if(verb != null)
			{
				Utils.drawString(g, verb, px, py + Game.ENTITY_TILE_SIZE, Utils.ANCHOR_HCENTER|Utils.ANCHOR_BOTTOM);
			}
		}
	}

	public String getInfo()
	{
		return null;
	}
}
